package cc.fs.video.myvideoproject

import android.app.Application
import android.provider.MediaStore
import cc.fs.video.myvideoproject.beans.FileBean
import cc.fs.video.myvideoproject.beans.VideoBean
import cc.fs.video.myvideoproject.utils.DatabaseHelper
import java.util.*

/**
 * Created by fostion on 2015/10/13.
 */
class AppContext : Application() {

    companion object {
        val selectFiles: ArrayList<FileBean> = ArrayList();
        val videos: ArrayList<VideoBean> = ArrayList();
        var instanceTem: AppContext ? = null
        val instance: AppContext by lazy {
            instanceTem!!
        }

        fun isContainFile(currentFile: FileBean): Boolean {
            for (file in selectFiles) {
                if (file.name!!.equals(currentFile.name))
                    return true
            }
            return false
        }

        fun addSelectFile(currentFile: FileBean) {
            if (!isContainFile(currentFile)) {
                selectFiles.add(currentFile)
                databaseHelper.saveFile(currentFile)
            }
        }

        fun removeSelectFile(currentFile: FileBean) {
            for (file in selectFiles) {
                if (file.name!!.equals(currentFile.name)) {
                    selectFiles.remove(file)
                    databaseHelper.removeFile(currentFile)
                    return
                }
            }
        }

        fun getVideoList():ArrayList<VideoBean>{
            return videos
        }

        fun getVideo() {
            val cursor = instance.contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, null)

            cursor.moveToFirst()
            while (!cursor.isAfterLast) {
                val video: VideoBean = VideoBean()
                video.id = "" + cursor.getInt(0)//cursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.BUCKET_ID))
                video.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE))
                video.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA))
                video.mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.MIME_TYPE))
                video.duration = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.DURATION))
                video.size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE))
//                //get thumb
                val section = MediaStore.Video.Thumbnails.VIDEO_ID + "=" + video.id
                val thumbCursor = instance.contentResolver.query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, null, section, null, null)
                if (thumbCursor.moveToFirst()) {
                    video.imgPath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA))
                }
                videos.add(video)
                cursor.moveToNext()
            }
        }

        fun findVideo(path:String): VideoBean?{
            for(video in videos){
                if(path.equals(video.path)){
                    return video
                }
            }
            return null
        }

        fun clearMemory(){
            selectFiles.clear()
        }

        //init databaseHelper
        val databaseHelper: DatabaseHelper by lazy {
            DatabaseHelper(instance)
        }
    }

    override fun onCreate() {
        super.onCreate()
        instanceTem = this
        selectFiles.clear()
        videos.clear()
        selectFiles.addAll(databaseHelper.getFiles())
        getVideo()
    }
}